﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Web.UI.HtmlControls;
public partial class Main_Master_Currency : System.Web.UI.Page
{
    Datautility DUCur = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {           
           if (Request.QueryString["View"] != null)
           {
               if (Request.QueryString["View"].ToString() == "True")
               {
                   pnlview.Visible = true;
                   pnlmain.Visible = false;
                   ViewData();
               }
           }
           else
           {
               //Getdata();
               pnlview.Visible = false;
           }
        }
    }
    //protected void Getdata()
    //{
    //    string sql = "select * from Mas_Currency";
    //    DataTable dtcur = DUCur.getdata(sql);
    //    if (dtcur.Rows.Count > 0)
    //    {
    //        grdcurrncy.DataSource = dtcur;
    //        grdcurrncy.DataBind();
    //    }
    //}
    protected void btncurrency_Click(object sender, EventArgs e)
    {
        SqlParameter[] param = {new SqlParameter("@currency_name",txtcurrency.Text.Trim())};
        int result = DUCur.executeprocedure("ACC_MAS_Currency", param);
        if (result > 0)
        {
            //Getdata();
            txtcurrency.Text = "";
        }
    }
    protected void ViewData()
    {
        string sql = "select * from Mas_Currency";
        DataTable dtcur = DUCur.getdata(sql);
        if (dtcur.Rows.Count > 0)
        {
            grdview.DataSource = dtcur;
            grdview.DataBind();
        }
    }
    protected void grdview_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdview.PageIndex = e.NewPageIndex;
        ViewData();
    }
    //protected void custPager_PageChanged(object sender, CustomPageChangeArgs e)
    //{
    //    grdcurrncy.PageSize = e.CurrentPageSize;
    //    grdcurrncy.PageIndex = e.CurrentPageNumber;
    //    Getdata();
    //}
    protected void grdview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdview.EditIndex = -1;
        ViewData();
    }
    protected void grdview_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdview.EditIndex = e.NewEditIndex;
        ViewData();
    }
    protected void grdview_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = grdview.DataKeys[e.RowIndex].Value.ToString();
        TextBox txtcurName = (TextBox)grdview.Rows[e.RowIndex].FindControl("txtcurrency");
        string sql = "update MAS_CURRENCY set currency_name='"+txtcurName.Text+"' where id="+id;
        DUCur.executesql(sql);
        grdview.EditIndex = -1;
        ViewData();
    }
    protected void imgxl_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment;filename=Currency_Report.xls");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            HtmlForm hf = new HtmlForm();
            grdview.Parent.Controls.Add(hf);
            hf.Attributes["runat"] = "server";

            grdview.Columns[grdview.Columns.Count - 1].Visible = false;
            grdview.AllowPaging = false;
            grdview.HeaderRow.Style.Add("width", "15%");
            grdview.HeaderRow.Style.Add("font-size", "10px");
            grdview.Style.Add("text-decoration", "none");
            grdview.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            grdview.Style.Add("font-size", "8px");
            hf.Controls.Add(grdview);
            hf.RenderControl(htw);

            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {

        }
    }
    protected void imgword_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ContentType = "application/vnd.word";
            Response.AddHeader("content-disposition", "attachment;filename=Currency_Report.doc");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            HtmlForm hf = new HtmlForm();
            grdview.Parent.Controls.Add(hf);
            hf.Attributes["runat"] = "server";

            grdview.Columns[grdview.Columns.Count - 1].Visible = false;
            grdview.AllowPaging = false;
            grdview.HeaderRow.Style.Add("width", "15%");
            grdview.HeaderRow.Style.Add("font-size", "10px");
            grdview.Style.Add("text-decoration", "none");
            grdview.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            grdview.Style.Add("font-size", "8px");
            hf.Controls.Add(grdview);
            hf.RenderControl(htw);

            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {

        }
    }
    protected void imgpdf_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=Currency_Report.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            HtmlForm hf = new HtmlForm();
            grdview.Parent.Controls.Add(hf);
            hf.Attributes["runat"] = "server";

            grdview.Columns[grdview.Columns.Count - 1].Visible = false;
            grdview.AllowPaging = false;  
            grdview.HeaderRow.Style.Add("width", "15%");
            grdview.HeaderRow.Style.Add("font-size", "10px");
            grdview.Style.Add("text-decoration", "none");
            grdview.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            grdview.Style.Add("font-size", "8px");
            //grdview.DataBind();

            hf.Controls.Add(grdview);
            hf.RenderControl(htw);
                     
            //grdview.RenderControl(htw);           
            StringReader sr = new StringReader(sw.ToString());
            Document pdf = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdf);
            PdfWriter.GetInstance(pdf, Response.OutputStream);
            pdf.Open();
            htmlparser.Parse(sr);
            pdf.Close();
            Response.Write(pdf);
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        base.VerifyRenderingInServerForm(control);
    }
}